/**
 * Tile class contains units and handles map events
 */
Tile = new Class({
	
	_type : "Tile",
	
	/**
	 * Wrapper for tile and contents
	 * @var Element
	 */
	element : null,
	
	/**
	 * Button for event handling
	 * @var Element
	 */
	button : null,
	
	/**
	 * @var int x coordinate
	 */
	x : null,
	
	/**
	 * @var int y coordinate
	 */
	y : null,
	
	initialize : function(x,y,options){
		var obj = this;
		this.x = x;
		this.y = y;
		this.element 	= new Element('div',{'x':x,'y':y,'class':'tile'});
		this.button 	= new Element('a',{
			'title'	:'Coordinates ['+(x)+','+(y)+']',
			'events':{
				'click' : function(){
					obj.selectUnit();
				}
			}
		});
		this.element.adopt(this.button); 
	},
	
	selectUnit : function(){
		// @todo add support for stacked units
		//GameEng.getInstance().clearSelected();
		var units = this.getUnits();
		if(units[0]){
			units[0].toggleSelect();
			return units[0];
		}
	},
	
	toElement : function(){
		return this.element;
	},
	
	setAsPossibleMove : function(){
		this.button.addClass('possibleMove');
	},
	
	/**
	 * Get any units which should be contained within tile
	 */
	getUnits : function(){
		var map = GameEng.getInstance().map;
		return map.units[this.x][this.y];
	}
});